/**
 * List of {tblName} [tblTitle] .
 * Created by buzhizhe CodeConvert on {date}.
 */

var VM=new Object();
ApiHost += "/{subFolder}";

$(function(){
    initPage();
    loadData();
});

//初始化页面
function initPage() {
    VM = new Vue({
        delimiters: ["[[", "]]"],
        el: '#app',
        data: {listdata: [],pageindex:1,total:0}
    });
    $("#pageindex").val(0);
}

//加载数据
function loadData() {
    getListData(1);
}

//获取列表数据
function getListData(pageindex) {
    if (pageindex == undefined) pageindex = 1;
    $("#pageindex").val(pageindex);
    $("#pagesize").val(Page.pagesize);

    var url = ApiHost + "/{tblName}list";
    var formData = $('#formquery').serialize();
    $.ajax({
        type: "POST",
        url: url,
        dataType: "json",
        data:  formData ,
        error: function (request) {
            ajaxError(request,$(this));
        },
        success: function (result) {
            //console.log(result);
            ajaxSuccess(result, $(this));
            VM.listdata = result.data.rows;
			VM.pageindex=pageindex;
			VM.total=result.data.total;
			dealData();
        }
    });
}

//处理数据
function dealData() {
	pagerInit(VM.pageindex, VM.total);
}

//打开编辑窗口
function popupEdit(id) {
    var url = ApiHost + "/{tblName}add/?id="+id;
    openLayer(url,"修改");
}
//打开查看窗口
function popupView(id) {
    var url = ApiHost + "/{tblName}view/?id="+id;
    openLayer(url,"查看详情");
}

//删除记录
function popupDelete(id) {
    if(!confirm("确定要删除此记录吗？删除后不可恢复！")){
		return ;
	}
	var url = ApiHost + "/{tblName}delete";
	$.ajax({
		type: "post",
		url: url,
		data: { 
			id:id 
		},
		dataType: "json",
		error: function (request) {
			ajaxError(request, $(this));
		},
		success: function (result) {
			ajaxSuccess(result, $(this));
			var code=result.statecode;
			if(code==0){
				//layerAlert("删除成功");
				layerCallBack();
			}else{
				layerAlert("提交失败："+result.message);
			}
		}
	}
	);
}

//回调函数
function layerCallBack() {
    setTimeout('loadData()',200);

}